package com.yao.yaooj.model.vo;

import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import com.yao.yaooj.judge.codesandbox.model.JudgeInfo;
import com.yao.yaooj.model.entity.QuestionSubmit;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;


/**
 * 题目提交VO
 * 用来返回给用户的，可以节约网络传输大小，或者过滤字段（脱敏），保证安全性
 * @TableName questionSubmit
 */
@Data
public class QuestionSubmitVO implements Serializable {

    private final static Gson GSON = new Gson();
    /**
     * id
     */
    private Long id;

    /**
     * 编程语言
     */
    private String language;

    /**
     * 用户代码
     */
    private String code;

    /**
     * 判题信息(JSON对象，便于前端去处理)
     */
    private JudgeInfo judgeInfo;

    /**
     * 判题状态(0-待判题；1-判题中；2-成功；3-失败)
     */
    private Integer status;

    /**
     * 题目 id
     */
    private Long questionId;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 提交用户信息
     */
    private UserVO userVO;

    /**
     * 对应题目信息
     */
    private QuestionVO questionVO;

    private static final long serialVersionUID = 1L;

    /**
     * 包装类转对象
     *
     * @param questionSubmitVO
     * @return
     */
    public static QuestionSubmit voToObj(QuestionSubmitVO questionSubmitVO) {
        if (questionSubmitVO == null) {
            return null;
        }
        QuestionSubmit questionSubmit = new QuestionSubmit();
        BeanUtils.copyProperties(questionSubmitVO, questionSubmit);//把同名属性做一个拷贝复制
        JudgeInfo vojudgeInfo= questionSubmitVO.getJudgeInfo();
        if(vojudgeInfo != null){
            questionSubmit.setJudgeInfo(JSONUtil.toJsonStr(vojudgeInfo));
        }

        return questionSubmit;
    }

    /**
     * 对象转包装类
     *
     * @param questionSubmit
     * @return
     */
    public static QuestionSubmitVO objToVo(QuestionSubmit questionSubmit) {
        if (questionSubmit == null) {
            return null;
        }
        QuestionSubmitVO questionSubmitVO = new QuestionSubmitVO();
        BeanUtils.copyProperties(questionSubmit, questionSubmitVO);

        String judgeInfo=questionSubmit.getJudgeInfo();
        if(judgeInfo != null){
            //把json字符串转成java对象,对象类型为JudgeConfig对象类型
            questionSubmitVO.setJudgeInfo(JSONUtil.toBean(judgeInfo, JudgeInfo.class));
        }
        return questionSubmitVO;
    }
}